package com.hqq.queue.service.impl;

import com.hqq.queue.service.QueueProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.annotation.Resource;
import javax.jms.*;

/**
 * QueueProducerImpl
 * Created by heqianqian on 2018/1/20.
 */
public class QueueProducerImpl implements QueueProducer {
    private static final Logger logger = LoggerFactory.getLogger(QueueProducerImpl.class);

    @Autowired
    private JmsTemplate jmsTemplate;

    @Resource(name = "queueDestination")
    private Destination queueDestination;

    @Override
    public void sendMessage(final String message) {
        jmsTemplate.send(queueDestination, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage();
                textMessage.setText(message);
                System.out.println("Send Queue Message:" + message);
                //TODO Spring集成slf4j
                //logger.info("Send Queue Message:{}", message);
                return textMessage;
            }
        });
    }
}
